package com.hu.bgp.server;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.AdminLogging.LogLevel;
import com.avaje.ebean.AdminLogging.LogLevelStmt;
import com.avaje.ebean.AdminLogging.LogLevelTxnCommit;
import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebean.config.ServerConfig;
import com.hu.bgp.server.domain.Profile;
import com.hu.bgp.server.domain.Score;

/**
 * Deze klasse initialiseert de connectie met de database na een aanroep van
 * init().
 * 
 * Alle bestaande tabellen en data worden hierbij verwijderd indien de
 * BgpConfiguration zo is ingesteld. Gebruikt daarna createTestData() om wat
 * testdata in de db te zetten.
 * 
 * @author ejvos
 */
public class Persistence
{
	private GameServerContext gsc;
	private DataSourceConfig defaultDsc;
	private ServerConfig defaultSc;
	private EbeanServer defaultServer;
	private DataSourceConfig scoreDsc;
	private ServerConfig scoreSc;
	private EbeanServer scoreServer;

	public Persistence(GameServerContext gsc)
	{
		this.gsc = gsc;

		// config for the default database
		defaultDsc = new DataSourceConfig();
		defaultDsc.setDriver("oracle.jdbc.driver.OracleDriver");
		defaultDsc.setUsername("V3IN01_0910_TEAM4");
		defaultDsc.setPassword("V3IN01_0910_TEAM4");
		defaultDsc.setUrl("jdbc:oracle:thin:@ondora01.hu.nl:8521:cursus01");

		defaultSc = new ServerConfig();
		defaultSc.setName("defaultdb");
		defaultSc.setDataSourceConfig(defaultDsc);
		defaultSc.setDdlGenerate(gsc.getConfiguration().getDoRebuildDatabase());
		defaultSc.setDdlRun(gsc.getConfiguration().getDoRebuildDatabase());
		defaultSc.setDefaultServer(false);
		defaultSc.setRegister(false);
		defaultSc.setDebugSql(false);
		disableLogging(defaultSc);

		// TODO voeg hier alle entities toe voor het default schema
		defaultSc.addClass(Profile.class);

		// config for the scores database
		scoreDsc = new DataSourceConfig();
		scoreDsc.setDriver("oracle.jdbc.driver.OracleDriver");
		scoreDsc.setUsername("stud1537275");
		scoreDsc.setPassword("stud1537275");
		scoreDsc.setUrl("jdbc:oracle:thin:@ondora01.hu.nl:8521:cursus01");

		scoreSc = new ServerConfig();
		scoreSc.setName("scoredb");
		scoreSc.setDataSourceConfig(scoreDsc);
		scoreSc.setDdlGenerate(gsc.getConfiguration().getDoRebuildDatabase());
		scoreSc.setDdlRun(gsc.getConfiguration().getDoRebuildDatabase());
		scoreSc.setDefaultServer(false);
		scoreSc.setRegister(false);
		disableLogging(scoreSc);

		// TODO voeg hier alle entities toe voor het score schema
		scoreSc.addClass(Score.class);

		// add some test data to the new database
		if (gsc.getConfiguration().getDoRebuildDatabase())
		{
			createTestData();
		}
	}

	private static void disableLogging(ServerConfig sc)
	{
		sc.setDebugSql(false);
		sc.setLoggingLevel(LogLevel.NONE);
		sc.setLoggingLevelIud(LogLevelStmt.NONE);
		sc.setLoggingLevelSqlQuery(LogLevelStmt.NONE);
		sc.setLoggingLevelQuery(LogLevelStmt.NONE);
		sc.setLoggingLevelTxnCommit(LogLevelTxnCommit.NONE);
	}

	public void init()
	{
		// zet het log level tijdelijk wat lager zodat dit de console niet vult
		// met onzin
		Logger topLogger = Logger.getLogger("");
		Level oldLevel = topLogger.getLevel();
		topLogger.setLevel(Level.WARNING);

		// initialiseer connecties en schema's
		defaultServer = EbeanServerFactory.create(defaultSc);
		scoreServer = EbeanServerFactory.create(scoreSc);

		// zet level weer terug op oude level
		topLogger.setLevel(oldLevel);
	}

	private void createTestData()
	{
	}

	public EbeanServer getDefaultServer()
	{
		if (defaultServer == null)
		{
			throw new IllegalStateException("persistence not initialized");
		}
		return defaultServer;
	}

	public EbeanServer getScoreServer()
	{
		if (scoreServer == null)
		{
			throw new IllegalStateException("persistence not initialized or scoreServer temporary disabled for speed");
		}
		return scoreServer;
	}
}
